#include "mysql.h"

sql::Connection *Mysql::conn_ = nullptr;

shared_ptr<User> Mysql::queryUserByUid(int uid)
{

    shared_ptr<User> user = std::make_shared<User>();
    sql::Statement *stmt = conn_->createStatement();

    string sql = "select * from user where uid = " + uid;
    sql::ResultSet *rs = stmt->executeQuery(sql);

    if(rs->next()){
        user->id = rs->getInt(1); 
        user->uid = rs->getInt(2);
        user->code = rs->getInt(3);
        user->precode = rs->getInt(4);
        user->fname = rs->getString(5);
        user->md5sum = rs->getString(6);
    }

#ifdef DEBUG
    cout << "queryUserById DEBUG..." << endl;
#endif

    return user;
}


Mysql::Mysql()
{
    sql::mysql::MySQL_Driver* driver =
                sql::mysql::get_mysql_driver_instance();
    conn_ = driver->connect(MYSQL_HOST_, MYSQL_USER_, MYSQL_PWD_);
    conn_->setSchema(MYSQL_DB_);
}
Mysql::~Mysql()
{
    delete conn_;
}